/**
 * 工具类1.0
 */
function tools() {

}

tools.prototype = {
	isWeixin: function() {
		var ua = window.navigator.userAgent.toLowerCase();
		if(ua.match(/MicroMessenger/i) == 'micromessenger') {
			return true;
		} else {
			return false;
		}
	},
	forEach: function(array, callback, scope) {
		for(var i = 0; i < array.length; i++) {
			callback.call(scope, i, array[i]);
		}
	},
	getQueryString: function(name, url) {
		var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
		if(!url || url == "") {
			url = window.location.search;
		} else {
			url = url.substring(url.indexOf("?"));
		}
		var r = url.substr(1).match(reg);
		if(r != null) return unescape(r[2]);
		return null;
	},
	setSession: function(key, value) {
		window.sessionStorage.setItem(key, value);
	},
	setOjbSession: function(key, value) {
		window.sessionStorage.setItem(key, this.getString(value));
	},
	getSession: function(key) {
		return window.sessionStorage.getItem(key);
	},
	getObjSession: function(key) {
		return this.getObject(window.sessionStorage.getItem(key));
	},
	removeSession: function(key) {
		window.sessionStorage.removeItem(key);
	},
	clearSession: function() {
		window.sessionStorage.clear();
	},
	getObject: function(str) {
		return eval("(" + str + ")");
	},
	getString: function(obj) {
		return JSON.stringify(obj);
	},
	setLocal: function(key, value) {
		window.localStorage.setItem(key, value);
	},
	setOjbLocal: function(key, value) {
		window.localStorage.setItem(key, this.getString(value));
	},
	getLocal: function(key) {
		return window.localStorage.getItem(key);
	},
	getObjLocal: function(key) {
		return this.getObject(window.localStorage.getItem(key));
	},
	removeLocal: function(key) {
		window.localStorage.removeItem("key");
	},
	clearLocal: function() {
		window.localStorage.clear();
	},
	getAge: function(identityCard) {
		var len = (identityCard + "").length;
		if(len == 0) {
			return 0;
		} else {
			if((len != 15) && (len != 18)) {
				return 0;
			}
		}
		var strBirthday = "";
		if(len == 18) {
			strBirthday = identityCard.substr(6, 4) + "/" + identityCard.substr(10, 2) + "/" + identityCard.substr(12, 2);
		}
		if(len == 15) {
			strBirthday = "19" + identityCard.substr(6, 2) + "/" + identityCard.substr(8, 2) + "/" + identityCard.substr(10, 2);
		}
		var birthDate = new Date(strBirthday);
		var nowDateTime = new Date();
		var age = nowDateTime.getFullYear() - birthDate.getFullYear();
		if(nowDateTime.getMonth() < birthDate.getMonth() || (nowDateTime.getMonth() == birthDate.getMonth() && nowDateTime.getDate() < birthDate.getDate())) {
			age--;
		}
		return age;
	},
	formatDate: function(date, fmt) {
		var o = {
			"M+": date.getMonth() + 1, // 月份
			"d+": date.getDate(), // 日
			"h+": date.getHours(), // 小时
			"H+": date.getHours(), // 小时
			"m+": date.getMinutes(), // 分
			"s+": date.getSeconds(), // 秒
			"q+": Math.floor((date.getMonth() + 3) / 3), // 季度
			"S": date.getMilliseconds()
			// 毫秒
		};
		if(/(y+)/.test(fmt))
			fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "")
				.substr(4 - RegExp.$1.length));
		for(var k in o)
			if(new RegExp("(" + k + ")", "g").test(fmt))
				fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) :
					(("00" + o[k]).substr(("" + o[k]).length)));
		return fmt;
	},
	addDate: function(date, dadd) {
		date = date.valueOf();
		date = date + dadd * 24 * 60 * 60 * 1000;
		return new Date(date);
	},
	getAndroid: function() {
		var u = navigator.userAgent;
		var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1;
		return isAndroid;
	},
	getIos: function() {
		var u = navigator.userAgent;
		var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/);
		return isiOS;
	},
	getPc: function() {
		if(/Android|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent)) {
			return false;
		}
		return true;
	},
	/**
	 * 去掉默认监听后，click事件需要用  touchstart事件代替
	 * 禁用屏幕滑动
	 */
	removeListener: function() {
		document.addEventListener("touchstart", function(e) {
			// 判断默认行为是否可以被禁用
			if(event.cancelable) {
				// 判断默认行为是否已经被禁用
				if(!event.defaultPrevented) {
					event.preventDefault();
				}
			}
		}, {
			passive: false
		});
	},
	/**
	 * 添加script
	 * @param {Object} url
	 */
	addScript: function(url) {
		var head = document.getElementsByTagName('head')[0];
		var script = document.createElement('script');
		script.type = 'text/javascript';
		script.charset = "UTF-8";
		script.src = url;
		head.appendChild(script);
	},
	/**
	 * 全屏
	 */
	fullScreen: function(el) {
		var element = el ? el : document.documentElement;
		if(element.requestFullscreen) {
			element.requestFullscreen();
		} else if(element.mozRequestFullScreen) { //FireFox
			element.mozRequestFullScreen();
		} else if(element.webkitRequestFullScreen) { //Chrome等
			element.webkitRequestFullScreen();
		} else if(element.msRequestFullscreen) { //IE11
			element.msRequestFullscreen();
		}
	},
	/**
	 * 退出全屏
	 */
	exitScreen: function() {
		if(document.exitFullscreen) {
			document.exitFullscreen();
		} else if(document.mozCancelFullScreen) {
			document.mozCancelFullScreen();
		} else if(document.webkitCancelFullScreen) {
			document.webkitCancelFullScreen();
		} else if(document.msExitFullscreen) {
			document.msExitFullscreen();
		}
	}
}